/*
 * CuboTorre.cpp
 *
 *  Created on: 18/04/2013
 *      Author: gonzalo
 */

#include "CuboTorre.h"

CuboTorre::CuboTorre(GLuint program, GLfloat largo_cubos, GLfloat ancho_cubos) :
		Figure(program) {
	for (int i = 0; i < 12; i++) {
		cubos[i] = new Cube(program);
	}

	cubos[0]->traslate(glm::vec3(0, -largo_cubos / 2, 0));
	cubos[0]->rotate(45, glm::vec3(1, 0, 0));
	cubos[0]->scale(glm::vec3(largo_cubos, ancho_cubos, ancho_cubos));

	cubos[1]->traslate(glm::vec3(0, largo_cubos / 2, 0));
	cubos[1]->rotate(45, glm::vec3(1, 0, 0));
	cubos[1]->scale(glm::vec3(largo_cubos, ancho_cubos, ancho_cubos));

	cubos[2]->traslate(glm::vec3(-largo_cubos / 2, 0, 0));
	cubos[2]->rotate(90, glm::vec3(0, 0, 1));
	cubos[2]->rotate(45, glm::vec3(1, 0, 0));
	cubos[2]->scale(glm::vec3(largo_cubos, ancho_cubos, ancho_cubos));

	cubos[3]->traslate(glm::vec3(largo_cubos / 2, 0, 0));
	cubos[3]->rotate(90, glm::vec3(0, 0, 1));
	cubos[3]->rotate(45, glm::vec3(1, 0, 0));
	cubos[3]->scale(glm::vec3(largo_cubos, ancho_cubos, ancho_cubos));

	cubos[4]->traslate(glm::vec3(largo_cubos / 2, largo_cubos / 2, largo_cubos / 2));
	cubos[4]->rotate(45, glm::vec3(0, 0, 1));
	cubos[4]->scale(glm::vec3(ancho_cubos, ancho_cubos, largo_cubos));

	cubos[5]->traslate(glm::vec3(-largo_cubos / 2, largo_cubos / 2, largo_cubos / 2));
	cubos[5]->rotate(45, glm::vec3(0, 0, 1));
	cubos[5]->scale(glm::vec3(ancho_cubos, ancho_cubos, largo_cubos));

	cubos[6]->traslate(glm::vec3(largo_cubos / 2, -largo_cubos / 2, largo_cubos / 2));
	cubos[6]->rotate(45, glm::vec3(0, 0, 1));
	cubos[6]->scale(glm::vec3(ancho_cubos, ancho_cubos, largo_cubos));

	cubos[7]->traslate(glm::vec3(-largo_cubos / 2, -largo_cubos / 2, largo_cubos / 2));
	cubos[7]->rotate(45, glm::vec3(0, 0, 1));
	cubos[7]->scale(glm::vec3(ancho_cubos, ancho_cubos, largo_cubos));

	cubos[8]->traslate(glm::vec3(0, -largo_cubos / 2, largo_cubos / 2));
	cubos[8]->rotate(-45, glm::vec3(0, 1, 0));
	cubos[8]->rotate(45, glm::vec3(1, 0, 0));
	cubos[8]->scale(glm::vec3(sqrt(largo_cubos * largo_cubos * 2), ancho_cubos, ancho_cubos));

	cubos[9]->traslate(glm::vec3(0, largo_cubos / 2, largo_cubos / 2));
	cubos[9]->rotate(45, glm::vec3(0, 1, 0));
	cubos[9]->rotate(45, glm::vec3(1, 0, 0));
	cubos[9]->scale(glm::vec3(sqrt(largo_cubos * largo_cubos * 2), ancho_cubos, ancho_cubos));

	cubos[10]->traslate(glm::vec3(-largo_cubos / 2, 0, largo_cubos / 2));
	cubos[10]->rotate(90, glm::vec3(0, 0, 1));
	cubos[10]->rotate(45, glm::vec3(0, 1, 0));
	cubos[10]->rotate(45, glm::vec3(1, 0, 0));
	cubos[10]->scale(glm::vec3(sqrt(largo_cubos * largo_cubos * 2), ancho_cubos, ancho_cubos));

	cubos[11]->traslate(glm::vec3(largo_cubos / 2, 0, largo_cubos / 2));
	cubos[11]->rotate(90, glm::vec3(0, 0, 1));
	cubos[11]->rotate(-45, glm::vec3(0, 1, 0));
	cubos[11]->rotate(45, glm::vec3(1, 0, 0));
	cubos[11]->scale(glm::vec3(sqrt(largo_cubos * largo_cubos * 2), ancho_cubos, ancho_cubos));

}

CuboTorre::~CuboTorre() {
	for (int i = 0; i < 12; i++) {
		delete cubos[i];
	}
}


void CuboTorre::set_color(const glm::vec3 & color) {
	for (int i = 0; i < 12; i++) {
		cubos[i]->set_color(color);
	}
}

void CuboTorre::draw(GLuint program, glm::mat4 & world_matrix) {
	glm::mat4 matriz = world_matrix * this->model_matrix;

	//this->calculate_and_bind_normal_matrix(matriz);

	// Bind Model Matrix
	GLuint location_model_matrix = glGetUniformLocation(program, "ModelMatrix");
	if (location_model_matrix >= 0) {
		glUniformMatrix4fv(location_model_matrix, 1, GL_FALSE, &matriz[0][0]);
	}

	for (int i = 0; i < 12; i++) {
		cubos[i]->draw(program,matriz);
	}

}
